package com.stay.emoticon;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;

import com.stay.emoticon.EmoticonView.OnEmoticonTapListener;

public class MyGallery extends Gallery{
	private MyGestureListener gestureListener;
	private OnEmoticonTapListener mOnEmoticonTap;
	private Context context;
	private HashMap<Integer, ArrayList<Integer>> emoticons = new HashMap<Integer, ArrayList<Integer>>();
	public int gridCount = 28;
	public int galleryCount = 3;

	public MyGallery(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	public MyGallery(Context context, AttributeSet paramAttributeSet) {
		super(context, paramAttributeSet);
		this.context = context;
		initView();
	}

	public MyGallery(Context context, AttributeSet paramAttributeSet, int paramInt) {
		super(context, paramAttributeSet, paramInt);
		this.context = context;
		initView();
	}
	
	private void initView() {
		ArrayList<Integer> temp = null;
		for (int i = 0; i < galleryCount; i++) {
			temp = new ArrayList<Integer>();
			for (int j = 0; j < gridCount; j++) {
				if (R.drawable.mr_100 + j + gridCount * i > R.drawable.mr_182) {
					break;
				}
				temp.add(R.drawable.mr_100 + j + gridCount*i);
			}
			emoticons.put(i, temp);
		}
		gestureListener = new MyGestureListener(context, null, this);
		GalleryAdapter adapter = new GalleryAdapter();
		this.setAdapter(adapter);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		int kEvent;
		if (isScrollingLeft(e1, e2)) {
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		onKeyDown(kEvent, null);
		return true;
	}

	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() - e1.getX()  > 50;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
//		return gestureListener.getDector().onTouchEvent(event); 
	}
	
	
	class GalleryAdapter extends BaseAdapter {
		private ViewHolder holder;

		@Override
		public int getCount() {
			return galleryCount;
		}
		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null || convertView.getTag() == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.gridview, null);
				holder.gridView = (GridView) convertView.findViewById(R.id.GridView01);
				holder.gridView.setOnItemClickListener(new OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						mOnEmoticonTap.onEmoticonTap(Integer.parseInt(parent.getItemAtPosition(position).toString()));
					}
				});
				holder.gridView.setOnTouchListener(gestureListener);  
				convertView.setTag(holder);
				GridAdpater gridAdapter = new GridAdpater(emoticons.get(position));
				holder.gridView.setAdapter(gridAdapter);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			return convertView;
		}
	}
	
	static class ViewHolder{
		GridView gridView;
		ImageView emoction;
	}
	
	class GridAdpater extends BaseAdapter {
		ArrayList<Integer> resIDs = null;
		private ViewHolder holder;

		public GridAdpater(ArrayList<Integer> resID) {
			this.resIDs = resID;
		}

		public int getCount() {
			return resIDs.size() ;
		}

		public Object getItem(int position) {
			return resIDs.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null || convertView.getTag() == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.emoticon_item, null);
				holder.emoction = (ImageView) convertView.findViewById(R.id.emoticon_img);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.emoction.setImageResource(resIDs.get(position));
			return convertView;
//			ImageView imageView;
//			if (convertView == null) {
//				imageView = new ImageView(this.context);
//				imageView.setLayoutParams(new GridView.LayoutParams(40, 40));
//				imageView.setAdjustViewBounds(false);
//				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//				imageView.setPadding(2, 2, 2, 2);
//			} else {
//				imageView = (ImageView) convertView;
//			}s
//
//			imageView.setImageResource(resIDs.get(position));
//			return imageView;
		}
	}

	public void setOnEmoticonTap(OnEmoticonTapListener onEmoticonTap) {
		this.mOnEmoticonTap = onEmoticonTap;
	}


}
